from celery import Celery
from django.core.mail import send_mail
from django.conf import settings

import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dailyfresh.settings')
django.setup()

app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379')

app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


# 定义任务函数
@app.task(bind=True)
def send_register_active_email(to_email, username, token):
    # 发邮件
    subject = '天天生鲜欢迎信息'
    message = ''
    sender = settings.EMAIL_FROM
    receiver = [to_email]
    html_message = f'<h1>{username},欢迎您注册成为天天生鲜欢会员</h1>请点击以下链接激活你的账户<a href="http://127.0.0.1:8001/user/active/{token}">http://127.0.0.1:8001/user/active/{token}</a>'
    send_mail(subject, message, sender, receiver, html_message=html_message)
